package com.example.zuoye.controller;

import com.example.zuoye.entity.Cart;
import com.example.zuoye.resultentity.ResultDT;
import com.example.zuoye.service.CartService;
import com.example.zuoye.utils.ResultDTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    CartService cartService;


    /**
     * 添加商品到购物车中
     * @param objId
     * @param userId
     * @return
     */
    @RequestMapping("/addCartGood")
    public boolean addCartGood(@RequestParam("objId") String objId,
                               @RequestParam("userId") String userId){
        Cart cart = cartService.checkedCartService(objId,userId);
        if (cart != null){
            cartService.deleteCartService(objId,userId);
            return false;
        }else{
            cartService.getListCartService(userId);
            cartService.addCartService(objId,userId);
            return true;
        }

    }

    /**
     * 删除商品从购物车中
     * @param objId
     * @param userId
     * @return
     */
    @RequestMapping("/deleteCartGood")
    public boolean deleteCartGood(@RequestParam("objId") String objId,
                                  @RequestParam("userId") String userId){
        return cartService.deleteCartService(objId,userId);
    }


    /**
     * 查看该用户购物车列表
     * @param userId
     * @return
     */
    @RequestMapping("/getListCenterIndexGood")
    public ResultDT getListCenterIndexGood(@RequestParam("userId") String userId){
        // 查询所有订单信息
        List<Cart> getListCartService = cartService.getListCartService(userId);
        System.out.println(getListCartService);
        return ResultDTUtils.success(getListCartService);
    }

    @RequestMapping("/checkedCartByObjIdAndUserId")
    public boolean checkedCartByObjIdAndUserId(@RequestParam("objId") String objId,
                                               @RequestParam("userId") String userId){
        Cart cart = cartService.checkedCartService(objId,userId);
        if (cart != null){
            return true;
        }else{
            return false;
        }

    }


}
